package com.sky.controller.admin;



import com.atom.module.common.result.PageResult;
import com.atom.module.common.result.Result;
import com.sky.pojo.dto.DishDTO;
import com.sky.pojo.dto.DishPageQueryDTO;
import com.sky.pojo.entity.Dish;
import com.sky.pojo.vo.DishVO;
import com.sky.service.DishService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@Slf4j
@Tag(name = "菜品相关接口")
@RequestMapping("/admin/dish")
@RestController
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * 新增菜品
     *
     * @param dto
     * @return
     */
    @Operation(summary = "新增菜品")
    @PostMapping
    public Result<?> addDish(@RequestBody DishDTO dto) {
        log.info("新增菜品：{}", dto);
        dishService.addDish(dto);
        // 缓存优化---清理缓存
        redisTemplate.delete("dish_" + dto.getCategoryId());
        return Result.success();
    }

    @Operation(summary = "删除菜品")
    @DeleteMapping
    public Result<List<DishVO>> delete(@RequestParam List<Long> ids) {
        log.info("删除菜品：{}", ids);
        List<DishVO> delete = dishService.delete(ids);

        // 缓存优化---清理缓存--将菜品缓存全部删除
//        Set keys = redisTemplate.keys("dish_*");
//        redisTemplate.delete(keys);
        // 之前教程删除全部的缓存 以及如果发现待删除的内容存在 enable或者和套餐关联时 删除失败,
        // 修改后的 , 查询 disable 和 没绑定套餐的 , 进行删除, 并返回 , 在redis中删除对应的 分类id

        delete.forEach(item -> redisTemplate.delete("dish_" + item.getCategoryId()));

        return Result.success(delete);
    }

    /**
     * 分页查询菜品列表
     *
     * @param dto
     * @return
     */
    @Operation(summary = "分页查询菜品列表")
    @GetMapping("/page")
    public Result<PageResult> page(DishPageQueryDTO dto) {
        log.info("分页查询菜品列表：{}", dto);
        PageResult pageResult = dishService.page(dto);
        return Result.success(pageResult);
    }


    /**
     * 回显菜品
     *
     * @param id
     * @return
     */
    @Operation(summary = "根据id查询菜品")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id) {
        log.info("回显菜品：{}", id);
        DishVO dishVO = dishService.getById(id);
        return Result.success(dishVO);
    }


    /**
     * 菜品起售、停售
     *
     * @param status 菜品状态：1为起售，0为停售
     * @param id
     * @return
     */
    @Operation(summary = "菜品启售停售")
    @PostMapping("/status/{status}")
    public Result<?> startOrStop(@PathVariable Integer status, Long id) {
        log.info("菜品起售、停售: status={}, id={}", status, id);
        dishService.startOrStop(status, id);

        // 缓存优化---清理缓存--将菜品缓存全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }

    /**
     * 修改菜品
     *
     * @param dto
     * @return
     */
    @Operation(summary = "修改菜品")
    @PutMapping
    public Result update(@RequestBody DishDTO dto) {
        log.info("修改菜品：{}", dto);
        dishService.update(dto);

        // 缓存优化---清理缓存--将菜品缓存全部删除
        Set keys = redisTemplate.keys("dish_*");
        redisTemplate.delete(keys);

        return Result.success();
    }


    @Operation(summary = "根据分类id查询菜品")
    @GetMapping("/list")
    public Result<List<Dish>> list(Long categoryId, String name) {
        log.info("根据分类id查询菜品: categoryId={}, name={}", categoryId, name);
        List<Dish> dishes = dishService.getByCategoryId(categoryId, name);
        return Result.success(dishes);
    }


}
